package com.yunban.project.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.template.mapper.TemplateStyleMapper;
import com.yunban.project.template.model.domain.TemplateStyle;
import com.yunban.project.template.service.TemplateStyleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lijiajun
 * @date: 2024-11-06
 * @version: 1.0
 */
@Service
public class TemplateStyleServiceImpl extends ServiceImpl<TemplateStyleMapper, TemplateStyle> implements TemplateStyleService {

    @Override
    public List<TemplateStyle> findByStyleIdIn(List<Long> styleIds) {
        LambdaQueryWrapper<TemplateStyle> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TemplateStyle::getStyleId, styleIds);
        return this.list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    public void batchSave(long templateId, List<Long> styleIds) {
        if (!CollectionUtils.isEmpty(styleIds)) {
            List<TemplateStyle> list = new ArrayList<>();
            styleIds.forEach(styleId -> {
                TemplateStyle templateStyle = new TemplateStyle();
                templateStyle.setTemplateId(templateId);
                templateStyle.setStyleId(styleId);
                list.add(templateStyle);
            });
            this.saveBatch(list);
        }
    }

    @Override
    public void removeByTemplateId(long templateId) {
        LambdaQueryWrapper<TemplateStyle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateStyle::getTemplateId, templateId);
        this.remove(wrapper);
    }
}
